package user

import (
	"gq_volunteer_platform/bean"
	"gq_volunteer_platform/model/activity"
	"gq_volunteer_platform/model/user"

	"github.com/gin-gonic/gin"
)

func MyActivityList(c *gin.Context) {

	var req struct {
		Page      int    `binding:"required,gte=1,max=10000000"`
		Limit     int    `binding:"required,gte=1,max=10000000"`
		ModelType string `binding:"oneof=活动 培训"`
	}

	if err := c.ShouldBindQuery(&req); err != nil {
		bean.Log.Warning(err.Error())
		c.String(400, err.Error())
		return
	}

	var offset = (req.Page - 1) * req.Limit

	// 获取登录用户对象
	var u user.User
	if err := u.Get(c); err != nil {
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	}

	var count int64
	var list []struct {
		ID             uint
		OrganizationId uint
		Name           string
		Address        string
		StartTime      string
		EndTime        string
		ServeHour      uint
		Status         string

		ExitCheckStatus         string
		ActivityUserCheckStatus string // 用户审核状态
	}

	var tx = bean.MysqlDb.Model(&activity.Activity{}).
		Select("`activity`.`id`,`activity`.`organization_id`,`activity`.`name`,`activity`.`Address`,`activity`.`start_time`,`activity`.`end_time`,`activity`.`serve_hour`,`activity`.`status`,`activity_user`.`check_status` as activity_user_check_status,`activity_user`.`exit_check_status`").
		Joins("INNER JOIN `activity_user` on `activity_user`.`activity_id` = `activity`.`id` AND `activity_user`.`user_id`=?", u.ID).
		Where("`activity`.`model_type`=?", req.ModelType)

	// 查询总数
	if err := tx.Count(&count).Error; err != nil {
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	}

	// 查询列表
	if err := tx.Limit(req.Limit).Offset(offset).Order("`id` DESC").Find(&list).Error; err != nil {
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	}

	c.PureJSON(200, gin.H{
		"count": count,
		"list":  list,
	})

}
